package com.liang.demo01;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @Author:liangli
 * @Date:2020/11/17 10:44
 */
//实现多线程同步下载图片
public class TestThread2 extends Thread{
    private String url;
    private String name;
    public TestThread2(String url,String name){
        this.name=name;
        this.url=url;
    }
    @Override
    public void run() {
        WebDownloader downloader=new WebDownloader();
        downloader.downloader(url,name);
        System.out.println("下载的图片名为"+name);
    }

    public static void main(String[] args) {
        TestThread2 t1=new TestThread2("https://pics0.baidu.com/feed/37d3d539b6003af35ed8cbb408c25f5b1238b6f7.jpeg?token=20137b735026d772d22affcc7ed472dd","1.png");
        TestThread2 t2=new TestThread2("https://pics6.baidu.com/feed/3bf33a87e950352ae94044a110ab62f5b3118b61.jpeg?token=d0f2078e353de1a67cc8c8091e02c81b","2.png");

        TestThread2 t3=new TestThread2("https://pics1.baidu.com/feed/5882b2b7d0a20cf4f1c71f9732e1d231adaf9917.jpeg?token=f6bcae9605040ee493ebe2146c371611","3.png");

        t1.start();
        t2.start();
        t3.start();
    }
}
//下载器
 class WebDownloader{
    //下载方法
    public void downloader(String url,String name){
        try {
            FileUtils.copyURLToFile(new URL(url),new File(name));
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("io异常，downloader出现问题");
        }
    }
}

